<?php
class ProductGroupMapper
{
    public function __construction()
    {
        
    }
    
    public function writeProductGroupInDB($attr)
    {
       $addedProductCounter = 0;
       $updatedProductCounter = 0;
       $errorCount = 0;

       if($attr == "all")
       {
           //can't delete
           foreach( $this->xmlObj()->items as $key=>$value )
           {
               foreach( $value as $inKey=>$inValue )
               {
                   $codesArray = $this->searchCodeInDB($inValue['code']);
                               
                   if( count($codesArray) < 2  )
                   {                                      
                       $codeInDB = $codesArray[0];
                       
                       if( $codeInDB ) //component is exist
                       {
                           //$this->updateRowInDB( $codeInDB, array('name'=>$inValue['name'], 'discount_price'=>$inValue['discount_price'], 'weight'=>$inValue['weight'] ));
                           $updatedProductCounter++;                   
                       }
                       else // component not exist in DB
                       {   
                          //$prodArray = array( 'code'=>'33', 'name'=>'Pablo', 'discount_price'=>'200' ); 
                          $this->addProductInDB( array( 'code'=>$inValue['code'], 'name'=>$inValue['name'], 'price'=>$inValue['price'], 'discount_price'=>$inValue['discount_price'], 'weight'=>$inValue['weight']) );
                          
                          $addedProductCounter++;    
                       }    
                   }
               }
           }
       }
       elseif($attr == "last")
       {
          $yesterdayTime = time() - 3600 *24;
           
           foreach( $this->xmlObj()->items as $key=>$value )
           {
               foreach( $value as $inKey=>$inValue )
               {
                   $d = $this->convertDate($inValue['ts']);
                   
                   if( $d > $yesterdayTime)
                   {   
                       $codesArray = $this->searchCodeInDB($inValue['code']);

                       if( count($codesArray) < 2  )
                       {                                      
                           $codeInDB = $codesArray[0];

                           if( $codeInDB ) //component is exist
                           {
                               //$this->updateRowInDB( $codeInDB, array('name'=>$inValue['name'], 'discount_price'=>$inValue['discount_price'], 'weight'=>$inValue['weight'] ));
                               $updatedProductCounter++;                   
                           }
                           else // component not exist in DB
                           {                                 
                               //$prodArray = array( 'code'=>'33', 'name'=>'Pablo', 'discount_price'=>'200' ); 
                              $this->addProductInDB( array( 'code'=>$inValue['code'], 'name'=>$inValue['name'], 'price'=>$inValue['price'], 'discount_price'=>$inValue['discount_price'], 'weight'=>$inValue['weight']) );

                              $addedProductCounter++;    
                           }    
                       }
                   }
               }
           }           
       }
       
       return array($addedProductCounter, $updatedProductCounter);
   }
    
     private function getAllProducts()
   {
       $query = Doctrine_Query::create()
        ->from('Product p')
	->select('p.code');

        $data = $query->fetchArray();
        
        return $data;       
   }
   
   public function searchCodeInDB($code)
   {
       
       $query = Doctrine_Query::create()
        ->from('Product p')
	->select('p.id')
	->where('p.code = ?', $code);

        $data = $query->fetchArray();

        return $data;
   }
      
   public function addProductInDB($prodArray)   //$this->addProductInDB( array($inValue['code'], $inValue['name'], $inValue['discount_price'] ) ); //code, name, discount_price, weight
   {
       $addNewProduct = new Product();
       $addNewProduct['code'] = $prodArray['code'];
       $addNewProduct['Translation']['et']['name'] = $prodArray['name'];
       $addNewProduct['price'] = $prodArray['price'];
       $addNewProduct['discount_price'] = $prodArray['discount_price'];
       //$addNewProduct['weight'] = $prodArray['weight'];
       
       $addNewProduct->save();
       
       //return true; 
   }
   
   public function convertDate($date)
   {
       if($date)
       {       
           $date_elements  = explode("-", $date);

           $time = mktime(0,0,0,$date_elements[1],$date_elements[2],$date_elements[0]);
           //$time = mktime (0,0,0,12,20,2009);
           return $time;
       }
       else
       {
           return time();
       }
           
   }
   
   private function deleteProductFromDB($prod_id)
   {   
       Doctrine_Query::create()
        ->delete('Product p')
        ->where('p.id = ?', $prod_id )
        ->execute();       
   }
   
   public function initSimpleXMLObj()
   {
        $this->categoriesXML = $this->response;       
        return $this->categoriesXML;
   }
   
   public function xmlObj()
   {
       return $this->response;       
   }
    
}